package icu.lookyousmileface.singlemode;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author starrysky
 * @title: SingleTask
 * @projectName Juc_Pro
 * @description: 单例模式使用
 * @date 2021/1/304:50 下午
 */
public class SingleTask {
    public static void main(String[] args) {
        /**
         * 饿汉模式测试
         */
//        Hungry instance1 = Hungry.getInstance();
//        Hungry instance2 = Hungry.getInstance();
//        System.out.println(instance1);
//        System.out.println(instance2);

        /**
         * 懒汉模式测试
         */

//        LazyMan instance1 = LazyMan.getInstance();
//        LazyMan instance2 = LazyMan.getInstance();
//        System.out.println(instance1);
//        System.out.println(instance2);

        /**
         * 反射破坏
         */
//        try {
//            Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
//            declaredConstructor.setAccessible(true);
//            LazyMan lazyMan = declaredConstructor.newInstance(null);
//            System.out.println(lazyMan);
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }
        /**
         * 静态内部类
         */
//        try {
//            Constructor<StaticClass> declaredConstructor = StaticClass.class.getDeclaredConstructor(null);
//            declaredConstructor.setAccessible(true);
//            StaticClass staticClass = declaredConstructor.newInstance(null);
//            System.out.println(staticClass);
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }
        /**
         * 枚举反射,反射不了枚举，都是实例，没有无参构造，暴力反射不行
         * Exception in thread "main" java.lang.IllegalArgumentException: Cannot reflectively create enum objects
         */
        try {
            Constructor<EmmmClass> declaredConstructor = EmmmClass.class.getDeclaredConstructor(String.class,int.class);
            declaredConstructor.setAccessible(true);
            EmmmClass emmmClass = declaredConstructor.newInstance(null);
            System.out.println(emmmClass);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
